package org.jmc.refmanager.domain.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.jmc.refmanager.domain.dao.RoleDao;
import org.jmc.refmanager.domain.model.Role;
import org.jmc.refmanager.domain.service.RoleManager;
import org.jmc.refmanager.security.SecurityHelper;
import org.jmc.refmanager.web.wicket.enumeration.RoleEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("roleManager")
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public class RoleManagerImpl implements RoleManager {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource(name="roleDao")
	private RoleDao roleDao;
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<Role> findAll() {
		if (SecurityHelper.isAdministrator()) {
			return roleDao.findAll();
		} else {
			return roleDao.findAllBut(RoleEnum.ROLE_ADMIN.name());
		}
	}

	/**
	 * La propagation NOT_SUPPORTED suspend la transaction en cours et 
	 * permets de ne pas propager les modifications dnas le cadre d'une 
	 * trabnsaction si cet objet est modifie. Seul un appel explicite
	 * e persist provoque une ecriture (non transactionnelle)
	 * {@inheritDoc}
	 */
	@Override
//	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Role findById(Integer id) {
		return roleDao.find(id);
	}

	/**
	 * {@inheritDoc}
	 */
	public Role findByIdForUpdate(Integer id) {
		return roleDao.find(id);
	}

	@Override
//	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Role findByRoleName(String roleName) {
		return roleDao.findByRoleName(roleName);
	}
}
